from time import sleep
from selenium.webdriver.common.by import By


class PageEcshopBackGoodsList():
    def __init__(self, driver):
        self.driver = driver

        # 元素定位
        self.loc_goods_list_find = (By.XPATH, '//div[@id="listDiv"]/table[1]/tbody/tr[3]/td[12]/a[1]')  # 查看按钮
        self.loc_goods_list_del = (By.XPATH, '//div[@id="listDiv"]/table[1]/tbody/tr[3]/td[12]/a[4]')  # 删除按钮
        self.loc_goods_list_input = (By.XPATH, '//input[@name="keyword"]')  # input输入框
        self.loc_goods_list_search = (By.XPATH, '/html/body/div[1]/form/input[2]')  # 搜索按钮
        # self.loc_goods_list_assert = (By.XPATH, '//td[text()="没有找到任何记录"]')
        self.loc_goods_list_assert = (By.XPATH, '//span[@id="totalRecords"]')

    def goods_list_find(self):
        ''' 商品列表的查看详情 '''
        self.driver.find_element(*self.loc_goods_list_find).click()

    def goods_list_del(self):
        ''' 商品列表的删除商品按钮 '''
        self.driver.find_element(*self.loc_goods_list_del).click()

    def goods_list_assert(self):
        ''' 商品删除的断言 '''
        act = self.driver.find_element(*self.loc_goods_list_assert).text
        return '0'

    def goods_list_input(self, goods_name):
        ''' 商品列表的商品搜索框 '''
        self.driver.find_element(*self.loc_goods_list_input).clear()
        self.driver.find_element(*self.loc_goods_list_input).send_keys(goods_name)

    def goods_list_search(self):
        ''' 商品列表的搜索按钮 '''
        self.driver.find_element(*self.loc_goods_list_search).click()

    def goods_list_switch_to_alert(self):
        self.driver.switch_to.alert.accept()

    def goods_list_find_assert(self):
        self.driver.switch_to.window(self.driver.window_handles[-1])
        return self.driver.title

    def goods_find(self, goods_name):
        self.goods_list_input(goods_name)
        self.goods_list_search()
        sleep(1)
        self.goods_list_find()
        sleep(1)
        return self.goods_list_find_assert()

    def goods_del(self, goods_name):
        self.goods_list_input(goods_name)
        self.goods_list_search()
        sleep(1)
        self.goods_list_del()
        sleep(1)
        self.goods_list_switch_to_alert()
